package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.mapper.DtsPermissionMapper;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsPermission;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsAdminService;
import com.qf.service.DtsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class DtsRoleServiceImpl implements DtsRoleService {

    @Autowired
    DtsRoleMapper roleMapper;

    @Override
    public Set<String> findRoleByIds(Integer[] roleIds) {
        LambdaQueryWrapper<DtsRole> qw = new LambdaQueryWrapper<>();
        qw.in(DtsRole::getId, roleIds);
        List<DtsRole> roles = roleMapper.selectList(qw);
        Set<String> roleStrs = roles.stream().map(role -> {
            return role.getName();
        }).collect(Collectors.toSet());

        return roleStrs;
    }

    @Override
    public List<DtsRole> findAll() {
        return roleMapper.selectList(null);
    }

    @Override
    public IPage<DtsRole> queryPage(Integer page, Integer limit, String sort, String desc) {

        IPage<DtsRole> iPage = new Page<>(page,limit);
        QueryWrapper<DtsRole> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(desc) && !StringUtils.isEmpty(sort)){
            if(desc.equalsIgnoreCase("desc")){
                queryWrapper.orderByDesc(sort);
            }else {
                queryWrapper.orderByAsc(sort);
            }
        }

        IPage<DtsRole> iPage1 = roleMapper.selectPage(iPage, queryWrapper);
        return iPage1;
    }

    @Override
    public DtsRole insert(DtsRole dtsRole) {
        dtsRole.setAddTime(new Date());
        dtsRole.setUpdateTime(new Date());
        dtsRole.setDeleted(false);
        roleMapper.insert(dtsRole);
        return dtsRole;
    }

    @Override
    public void update(DtsRole dtsRole) {
        roleMapper.updateById(dtsRole);
    }

    @Autowired
    DtsAdminMapper adminMapper;
    @Autowired
    DtsPermissionMapper permissionMapper;
    @Override
    public boolean delete(DtsRole dtsRole) {
        //角色关联了管理员的话不能被删除
        LambdaQueryWrapper<DtsAdmin> qw1 = new LambdaQueryWrapper<>();
        qw1.select(DtsAdmin::getRoleIds);
        List<DtsAdmin> dtsAdmins = adminMapper.selectList(qw1);
        Integer id = dtsRole.getId();
        for (DtsAdmin dtsAdmin : dtsAdmins) {
            Integer[] roleIds = dtsAdmin.getRoleIds();
            for (Integer roleId : roleIds) {
                if(roleId.equals(id)){
                    return false;
                }
            }
        }
        //角色关联权限的话不能删除
        LambdaQueryWrapper<DtsPermission> qw2 = new LambdaQueryWrapper<>();
        qw2.eq(DtsPermission::getRoleId,dtsRole.getId());
        int count = permissionMapper.selectCount(qw2);
        if(count>=1){
            return false;
        }
        //否则可以删除
        roleMapper.deleteById(dtsRole.getId());
        return true;
    }

    @Override
    public boolean isSuperAdmin(Integer roleId) {
        DtsRole dtsRole = roleMapper.selectById(roleId);
        if("超级管理员".equalsIgnoreCase(dtsRole.getName())){
            return true;
        }
        return false;
    }
}
